//
//  TCRouteViewController.m
//  Project_B
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 张闯. All rights reserved.
//

#import "TCRouteViewController.h"
#import "UIImageView+WebCache.h"
#import "AFNetworkHandler.h"
#import "TCdetailRoute.h"
#import "TCMYdetailRouteTableViewCell.h"
#import "TCMydetailSecondTableViewCell.h"
#import "MBProgressHUD.h"
#import "UIImage+GIF.h"
#define WIDTH [[UIScreen mainScreen] bounds].size.width / 375
#define HEIGHT [[UIScreen mainScreen] bounds].size.height / 667

@interface TCRouteViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)MBProgressHUD *HUD;
@end

@implementation TCRouteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dataArr = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
//    self.navigationItem.title = self.temp.title;
    [self getData];
    [self creattableview];
}
- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)creattableview
{
    self.tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[TCMYdetailRouteTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerClass:[TCMydetailSecondTableViewCell class] forCellReuseIdentifier:@"cellsecond"];
    self.tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375*WIDTH, 340*HEIGHT)];
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.mode = MBProgressHUDModeIndeterminate;
    

    //    self.tableView.tableHeaderView.backgroundColor = [UIColor orangeColor];
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375*WIDTH, 200*HEIGHT)];
    [self.tableView.tableHeaderView addSubview:image];
    [image sd_setImageWithURL:[NSURL URLWithString:self.temp.image] placeholderImage:[UIImage imageNamed:@""]];
    UIImageView *smallimage = [[UIImageView alloc]initWithFrame:CGRectMake((375/2-25)*WIDTH, 175*HEIGHT, 50*WIDTH, 50*HEIGHT)];
    [smallimage sd_setImageWithURL:[NSURL URLWithString:self.temp.club_img] placeholderImage:[UIImage imageNamed:@""]];
    [self.tableView.tableHeaderView addSubview:smallimage];
    smallimage.layer.cornerRadius = 25;
    smallimage.clipsToBounds = YES;
    UILabel *timelabel = [[UILabel alloc]initWithFrame:CGRectMake(10*WIDTH, 220*HEIGHT, 355*WIDTH, 40*HEIGHT)];
    [self.tableView.tableHeaderView addSubview:timelabel];
    timelabel.text = [NSString stringWithFormat:@"活动时间:%@",self.temp.times];
    [timelabel setFont:[UIFont systemFontOfSize:13]];
    timelabel.textColor = [UIColor grayColor];
    UILabel *typelabel = [[UILabel alloc]initWithFrame:CGRectMake(10*WIDTH, 260*HEIGHT, 355*WIDTH, 40*HEIGHT)];
    [typelabel setFont:[UIFont systemFontOfSize:13]];
    [self.tableView.tableHeaderView addSubview:typelabel];
    typelabel.text = [NSString stringWithFormat:@"活动类型:%@",self.temp.catename];
    typelabel.textColor = [UIColor grayColor];
    UILabel *titlelabel = [[UILabel alloc]initWithFrame:CGRectMake(10*WIDTH, 300*HEIGHT, 355*WIDTH, 40*HEIGHT)];
    [titlelabel setFont:[UIFont systemFontOfSize:13]];
    titlelabel.numberOfLines = 0;
    titlelabel.textColor = [UIColor grayColor];
    titlelabel.text = [NSString stringWithFormat:@"活动主题:%@",self.temp.title];
    [self.tableView.tableHeaderView addSubview:titlelabel];
}
- (void)getData
{
    NSString *str = [NSString stringWithFormat:@"http://api.lis99.com/v2/club/replylist/%@/?page=0?page=0&topic_id=%@", self.temp.detalid, self.temp.detalid];
    [AFNetworkHandler getUrl:str cookie:nil result:^(id result) {
        self.dataArr = [TCdetailRoute transformWithArray:result[@"data"][@"topiclist"]];
        [self.HUD hide:YES];
        [self.tableView reloadData];
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.dataArr[indexPath.row] topic_image]) {
        return 310*HEIGHT;
    }
    else
    {
        return 130*HEIGHT;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TCMYdetailRouteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.temp = self.dataArr[indexPath.row];
    if (![self.dataArr[indexPath.row] topic_image]) {
        TCMydetailSecondTableViewCell *cellsecond = [tableView dequeueReusableCellWithIdentifier:@"cellsecond"];
        cellsecond.selectionStyle = UITableViewCellSelectionStyleNone;
        cellsecond.temp = self.dataArr[indexPath.row];
        return cellsecond;
    }
   return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
